package com.jianjian.system.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jianjian.common.result.Result;
import com.jianjian.common.utils.MD5;
import com.jianjian.model.system.SysUser;
import com.jianjian.model.vo.SysUserQueryVo;
import com.jianjian.system.service.SysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author cool
 * @since 2022-11-06
 */
@Slf4j
@Api(tags = "用户管理接口")
@RestController
@RequestMapping("/admin/system/sysUser")
public class SysUserController {

    @Autowired
    private SysUserService sysUserService;

    //1.条件分页查询
    @ApiOperation("条件分页查询")
    @GetMapping("/{page}/{limit}")
    public Result findPageQueryUser(@ApiParam(name = "page", value = "当前页码", required = true)
                                    @PathVariable Long page,

                                    @ApiParam(name = "limit", value = "每页记录数", required = true)
                                    @PathVariable Long limit,

                                    @ApiParam(name = "roleQueryVo", value = "查询对象", required = false)
                                            SysUserQueryVo userQueryVo) {
        //插件page对象
        Page<SysUser> pageParam = new Page<>(page, limit);

        IPage<SysUser> pageModel = sysUserService.selectPage(pageParam, userQueryVo);

        //返回
        return Result.ok(pageModel);
    }

    /**
     * 2.添加用户
     */
    @ApiOperation(value = "添加用户")
    @PostMapping("save")
    public Result save(@ApiParam(name = "sysUser", value = "用户对象", required = true)
                       @RequestBody(required = true) SysUser sysUser) {

        //把输入的密码进行MD5加密
        sysUser.setPassword( MD5.encrypt(sysUser.getPassword()));

        if (StringUtils.isEmpty(sysUser.getUsername())) {
            return Result.fail("请输入用户名");
        }

//        QueryWrapper<SysUser> wrapper = new QueryWrapper<>();
//
//        wrapper.eq("username", sysUser.getUsername());
//
//        if (sysUserService.count(wrapper) > 0) {
//            return Result.fail("用户名已经存在");
//        }

        Boolean is_success = sysUserService.save(sysUser);
        return is_success ? Result.ok("添加成功") : Result.fail("添加失败");

    }

    /**
     * 3.修改用户
     */
    @ApiOperation(value = "修改用户")
    @PostMapping("update")
    public Result update(@ApiParam(name = "sysUser", value = "修改用户", required = true)
                         @RequestBody(required = true) SysUser sysUser) {
        if (StringUtils.isEmpty(sysUser.getId())) {
            return Result.fail("请传入id");
        }

        boolean b = sysUserService.updateById(sysUser);
        return b ? Result.ok("修改成功") : Result.fail();
    }

    //4.根据id查询
    @ApiOperation("根据id查询用户")
    @GetMapping("findUserById/{id}")
    public Result findRoleById(@ApiParam(name = "id", value = "用户id", required = true)
                               @PathVariable("id") Long id) {

        return Result.ok(sysUserService.getById(id));
    }

    //5.批量删除  json格式的数组 就是java中的集合List
    @ApiOperation(value = "批量删除接口")
    @DeleteMapping("removeBatchIds")
    public Result removeBatchIds(@ApiParam(name = "ids", value = "多用户id", required = true)
                                    @RequestBody(required = true) List<Long> ids){
       return sysUserService.removeByIds(ids) ? Result.ok("删除成功") : Result.fail();
    }

    //6.逻辑删除
    @ApiOperation(value = "逻辑删除接口")
    @DeleteMapping("remove/{id}")
    public Result remove(@PathVariable("id") long id ){

        return sysUserService.removeById(id) ? Result.ok("删除成功") : Result.fail();
    }

}
